package com.zhkc.wisdom.common.iot;

import com.zhkc.wisdom.common.configure.constant.ConfigureConstant;
import com.zhkc.wisdom.common.iot.properties.IotProperties;
import com.zhkc.wisdom.common.iot.service.IotDBService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.iotdb.isession.util.Version;
import org.apache.iotdb.session.pool.SessionPool;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;


@Slf4j
@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnProperty(prefix = ConfigureConstant.ZHKC_PROPERTIES_PREFIX
        + ".iot", name = "enabled", havingValue = "true", matchIfMissing = false)
public class IotConfiguration {

    @Resource
    private IotProperties iotProperties;

    @Bean
    public IotDBService getIotDBService(){
        return new IotDBService(getSessionPool(),iotProperties);
    }

    public SessionPool getSessionPool() {
        return new SessionPool.Builder()
                .maxSize(iotProperties.getMaxSize())
                .host(iotProperties.getHost())
                .port(iotProperties.getPort())
                .user(iotProperties.getUser())
                .password(iotProperties.getPassword())
                .connectionTimeoutInMs(iotProperties.getConnectionTimeoutInMs())
                .version(Version.V_0_13)
                .build();
    }

}